package com.squirrel.michale;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/12 7:25 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0880 {


    public String decodeAtIndex(String S, int K) {
        long size = 0;
        int N = S.length();

        // Find size = length of decoded string
        for (int i = 0; i < N; ++i) {
            char c = S.charAt(i);
            if (Character.isDigit(c))
                size *= c - '0';
            else
                size++;
        }

        for (int i = N-1; i >= 0; --i) {
            char c = S.charAt(i);
            K %= size;
            if (K == 0 && Character.isLetter(c))
                return Character.toString(c);

            if (Character.isDigit(c))
                size /= c - '0';
            else
                size--;
        }
        return "";
    }



    public String decodeAtIndex4(String s, int k) {
        char[] chars = s.toCharArray();
        StringBuffer result = new StringBuffer();
        StringBuffer seed = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (aChar >= 49 && aChar <= 57) {
                int count = Integer.parseInt(String.valueOf(aChar));
                result.setLength(0);
                while (count> 0) {
                    result.append(seed);
                    count--;
                }
                seed = new StringBuffer(result);
            }else{
                seed.append(aChar);
            }
        }
        return result.toString().substring(k-1,k);
    }


    public String decodeAtIndex2(String s, int k) {
        char[] chars = s.toCharArray();
        StringBuffer result = new StringBuffer();
        StringBuffer seed1 = new StringBuffer();
        int lastNumberIndex = -1;
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (aChar >= 49 && aChar <= 57) {
//                if (lastNumberIndex != i - 1) {
//                    result.append("*");
//                }
//                result.append(aChar).append("*");
                lastNumberIndex = i;
                int count = Integer.valueOf(String.valueOf(aChar));
                System.out.println("count==" + count);
                System.out.println("seed1=="+ seed1);
                while (count-1> 0) {
                    result.setLength(0);
                    result.append(seed1);
                    count--;
                }
                seed1 = new StringBuffer(result);
            }else{
                seed1.append(aChar);
            }
        }

        System.out.println(result);
        System.out.println("------------------");
//        String[] split = result.toString().split("\\*");
//
//
//        StringBuffer stringBuffer2 = new StringBuffer();
//        StringBuffer seed = new StringBuffer();
//        for (int i = 0; i < split.length; i++) {
//            String cell = split[i];
//            System.out.println(cell);
//            if (i == 0) {
//                seed.append(cell);
//                stringBuffer2.append(seed);
//            }
//            char[] chars1 = cell.toCharArray();
//            if (chars1[0] >= 49 && chars1[0] <= 57) {
//                int count = chars1[0];
//                while (count - 1 > 0) {
//                    stringBuffer2.append("");
//                }
//            } else {
//                seed.append(chars1[0]);
//            }
//
//        }

        return result.toString().substring(k-1,k);

    }

    public String decodeAtIndex3(String s, int k) {

        String[] split = s.split("[1-9]");


        char[] chars = s.toCharArray();
        String prefix = "";
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (prefix == "") {
                prefix = s.substring(0, i);
            }
            if (aChar >= 49 && aChar <= 57) {
                stringBuffer.append(prefix);
                int count = aChar;
                while (count - 1 > 0) {
                    stringBuffer.append(prefix);
                    prefix = stringBuffer.toString();
                }
            }
        }

        return String.valueOf(stringBuffer.toString().charAt(k));

    }

    public static void main(String[] args) {
        String s1 = "leet2code3";
        String s2 = "ha22";
        String s3 = "a2345678999999999999999";

        LeetCode0880 leetCode0880 = new LeetCode0880();

        System.out.println(leetCode0880.decodeAtIndex(s1, 10));
        System.out.println("=======================");
        leetCode0880.treatString(s2);
        leetCode0880.decodeAtIndex(s2,5);
        System.out.println("=======================");
//        leetCode0880.treatString(s3);
//        leetCode0880.decodeAtIndex(s3,1);
//        System.out.println("=======================");


    }

    private void treatString(String s) {

        String[] split = s.split("[1-9]");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
        }


    }
}
